package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.RentDeviceLink;
import com.ruoyi.system.service.IRentDeviceLinkService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库设备Controller
 * 
 * @author ruoyi
 * @date 2020-12-27
 */
@Controller
@RequestMapping("/system/link")
public class RentDeviceLinkController extends BaseController
{
    private String prefix = "system/link";

    @Autowired
    private IRentDeviceLinkService rentDeviceLinkService;

    @RequiresPermissions("system:link:view")
    @GetMapping()
    public String link()
    {
        return prefix + "/link";
    }

    /**
     * 查询出库设备列表
     */
    @RequiresPermissions("system:link:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RentDeviceLink rentDeviceLink)
    {
        startPage();
        List<RentDeviceLink> list = rentDeviceLinkService.selectRentDeviceLinkList(rentDeviceLink);
        return getDataTable(list);
    }

    /**
     * 导出出库设备列表
     */
    @RequiresPermissions("system:link:export")
    @Log(title = "导出出库设备列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult export(RentDeviceLink rentDeviceLink)
    {
        List<RentDeviceLink> list = rentDeviceLinkService.selectRentDeviceLinkList(rentDeviceLink);
        ExcelUtil<RentDeviceLink> util = new ExcelUtil<RentDeviceLink>(RentDeviceLink.class);
        return util.exportExcel(list, "link");
    }

    /**
     * 新增出库设备
     */
    @RequestMapping("/add")
    public String add(RentDeviceLink rentDeviceLink,ModelMap mmap) {
        mmap.put("rentDeviceLink", rentDeviceLink);
        return prefix + "/add";
    }

    /**
     * 新增保存出库设备
     */
    @RequiresPermissions("system:link:add")
    @Log(title = "新增保存出库设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(RentDeviceLink rentDeviceLink)
    {
        return toAjax(rentDeviceLinkService.insertRentDeviceLink(rentDeviceLink));
    }

    /**
     * 修改出库设备
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        RentDeviceLink rentDeviceLink = rentDeviceLinkService.selectRentDeviceLinkById(id);
        mmap.put("rentDeviceLink", rentDeviceLink);
        return prefix + "/edit";
    }
    /**
     * 修改出库设备
     */
    @GetMapping("/gotoReturnDevice/{id}")
    public String gotoReturnDevice(@PathVariable("id") Long id, ModelMap mmap)
    {
        RentDeviceLink rentDeviceLink = rentDeviceLinkService.selectRentDeviceLinkById(id);
        mmap.put("rentDeviceLink", rentDeviceLink);
        return prefix + "/return_device";
    }
    /**
     * 修改保存出库设备
     */
    @RequiresPermissions("system:link:edit")
    @Log(title = "修改保存出库设备", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(RentDeviceLink rentDeviceLink){
        return rentDeviceLinkService.updateRentDeviceLink(rentDeviceLink);
    }

    /**
     * 归还设备
     */
    @RequiresPermissions("system:link:edit")
    @Log(title = "归还设备", businessType = BusinessType.UPDATE)
    @PostMapping("/returnDevice")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult returnDevice(RentDeviceLink rentDeviceLink){
        return rentDeviceLinkService.returnDevice(rentDeviceLink);
    }

    /**
     * 撤销设备
     */
    @RequiresPermissions("system:link:edit")
    @Log(title = "撤销设备", businessType = BusinessType.UPDATE)
    @PostMapping("/revokeDevice")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult revokeDevice(RentDeviceLink rentDeviceLink){
        return rentDeviceLinkService.revokeDevice(rentDeviceLink);
    }

    /**
     * 删除出库设备
     */
    @RequiresPermissions("system:link:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(rentDeviceLinkService.deleteRentDeviceLinkByIds(ids));
    }

    /**
     * 按照设备类型统计，设备出租的次数
     * @return
     */
    @GetMapping("/queryEveryDeviceTypeRentCount")
    @ResponseBody
    public AjaxResult queryEveryDeviceTypeRentCount(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", rentDeviceLinkService.queryEveryDeviceTypeRentCount());
        return ajax;
    }
    /**
     * 按照设备型号，统计出租对多的前10名
     * @return
     */
    @GetMapping("/queryRentTopTenDevice")
    @ResponseBody
    public AjaxResult queryRentTopTenDevice(){
        AjaxResult ajax = new AjaxResult();
        ajax.put("code", 200);
        ajax.put("value", rentDeviceLinkService.queryRentTopTenDevice());
        return ajax;
    }

}
